package cn.com.lms.business.livecourse.persistence.repository;

import cn.com.lms.business.livecourse.persistence.entity.CourseChatRoom;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * CourseChatRoomRepository
 *
 * @author barry
 * @date 2020/3/21
 **/
@Repository
public interface CourseChatRoomRepository extends BaseRepository<CourseChatRoom, Integer> {

    // 查找已过期无用房间(最多5间)
    @Query(value = "SELECT * FROM bnz_course_chat_room WHERE course_expire_time<?1 limit 5", nativeQuery = true)
    List<CourseChatRoom> unlessCourseChatRooms(Date currentDate);

    // 对应课程的聊天室房间
    @Query(value = "SELECT * FROM bnz_course_chat_room  WHERE course_info_id=?1 limit 1",
            nativeQuery = true)
    CourseChatRoom courseChatRoom(long courseId);

    // 删除对应课程的聊天室房间
    @Modifying
    @Query(value = "DELETE FROM bnz_course_chat_room  WHERE course_info_id=?1", nativeQuery = true)
    int destroyCourseChatRoom(long courseId);

    // 释放对应课程的聊天室房间占用
    @Modifying
    @Query(value = "UPDATE bnz_course_chat_room SET course_expire_time=?2 WHERE course_info_id=?1", nativeQuery = true)
    int releaseRoom(long courseId, Date expireTime);

    // 更新房间过期时间
    @Modifying
    @Query(value = "UPDATE bnz_course_chat_room SET course_expire_time=?2 WHERE course_info_id=?1", nativeQuery = true)
    int updateRoomExpireTime(long courseId, Date expireTime);
}
